package com.ymatou.iapi.coupon.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.coupon.parameter.ReturnCouponBean;
import com.ymatou.iapi.coupon.parameter.UseCouponBean;
import com.ymatou.iapi.coupon.parameter.UseCouponInfo;
import com.ymatou.iapi.coupon.service.ReturnCouponCall;
import com.ymatou.iapi.coupon.service.UserReceiveCouponCall;
import com.ymatou.iapi.couponmanage.parameter.CreateCouponBean;
import com.ymatou.iapi.couponmanage.service.CreateCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceCallServiceV2;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.database.model.coupon;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.database.sqlwapper.couponbatchWapper;
//import com.ymttest.database.model.YmtCouponBatch;
//import com.ymttest.database.model.YmtCouponPrivateUserBound;
//import com.ymttest.database.model.YmtCouponSetting;
//import com.ymttest.database.model.YmtCouponTradeRecord;
//import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
//import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
//import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
//import com.ymttest.database.sqlwapper.YmtCouponTradeRecordWapper;
import com.ymttest.utils.TestDataManager;
/**
*还券
*FreeMaker Template自动生成代码
*<!--添加还券记录-->
		insert into ymt_CouponTradeRecord (OrderId, UserId, CouponCode, BatchId, TradeType)
		values (#{orderId,jdbcType=INTEGER},
		#{userId,jdbcType=INTEGER},
		#{couponCode,jdbcType=VARCHAR},
		#{batchId,jdbcType=INTEGER},
		#{tradeType,jdbcType=TINYINT})

		<!--增加用户优惠券使用剩余次数-->
		UPDATE a SET iCouponUsedCount=iCouponUsedCount+1 from Ymt_CouponPrivateUserBound a with (index  = IX_Ymt_CouponPrivateUserBound_sCouponCode)
		WHERE sCouponCode=#{couponCode, jdbcType=VARCHAR}
		AND iUserId=#{userId, jdbcType=INTEGER}

		<!--更新批次优惠券剩余使用次数-->
		UPDATE Ymt_CouponSetting SET iMaxUseTime=iMaxUseTime+1
		WHERE iCouponSettingId IN (SELECT iCouponSettingId FROM Ymt_CouponBatch WITH(NOLOCK) WHERE ibatchid=#{batchId, jdbcType=INTEGER})
*/
@TestSuite
public class Ts_ReturnCoupon{
	private static ReturnCouponBean returncouponBean;
	private static ReturnCouponCall returncouponCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("还券");
	}

	@Before
	public void caseUp() {
		returncouponBean=new ReturnCouponBean();
		returncouponCall=new ReturnCouponCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(returncouponCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnCoupon_001() {
		Logger.start(true,"还券-平台券-领取券");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CouponServiceCallServiceV2 CouponServiceCallServiceV2 = new CouponServiceCallServiceV2();
			
			CreateCouponBean createCouponBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= CouponServiceMCallServiceV2.createPlatformCoupon(createCouponBean);
			UserReceiveCouponCall userReceiveCouponCall = CouponServiceMCallServiceV2.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			String couponCode = userReceiveCouponCall.getUserReceiveResult().getCouponCode();
			couponbatch batch1 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			
			UseCouponBean useCouponBean = CouponServiceCallServiceV2.setUseCouponBean(userId, orderId, couponCode);
			CouponServiceCallServiceV2.useCoupon(useCouponBean);
			
			couponbatch batch2 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(couponCode);
			returncouponBean.setUserId(userId);
			returncouponBean.setMainOrderId(orderId);
			returncouponBean.setCouponCodes(CouponCodes);
			returncouponBean.setAppId("app");
			returncouponBean.setRequestId("1233344");			
			
			returncouponCall.setData(returncouponBean);
			returncouponCall.callService();		
			
			couponbatch batch3 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			//job处理
			//Thread.sleep(6000);
			//还券前已使用和接收数量
			Logger.verifyEquals(1, batch1.getUsedCount()-batch2.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(1, batch1.getReceivedCount()-batch2.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");
			//还券后已使用和接收数量
			Logger.verifyEquals(1, batch3.getUsedCount()-batch2.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(1, batch3.getReceivedCount()-batch2.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");
			
			coupon coupon=new couponWapper().selectByBatchId(batch1.getBatchId()).get(0);
			Logger.verifyEquals(false, coupon.getIsUsed(), "验证数据库coupon中IsUsed");
			Logger.verifyEquals(returncouponBean.getMainOrderId(), coupon.getMainOrderId(), "验证数据库coupon中MainOrderId");
			
			Logger.verifyEquals("操作成功！", returncouponCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("0", returncouponCall.getStatusCode1(), "验证返回StatusCode");

			Logger.verifyEquals("null", returncouponCall.getSellerId(), "验证返回sellerId");		
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnCoupon_002() {
		Logger.start(true,"还券-商家券");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CouponServiceCallServiceV2 CouponServiceCallServiceV2 = new CouponServiceCallServiceV2();
			
			CreateCouponBean createCouponBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= CouponServiceMCallServiceV2.createPlatformCoupon(createCouponBean);
			UserReceiveCouponCall userReceiveCouponCall = CouponServiceMCallServiceV2.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			String couponCode = userReceiveCouponCall.getUserReceiveResult().getCouponCode();
			
            couponbatch batch1 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			
			UseCouponBean useCouponBean = CouponServiceCallServiceV2.setUseCouponBean(userId, orderId, couponCode);
			CouponServiceCallServiceV2.useCoupon(useCouponBean);
			
			couponbatch batch2 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(couponCode);
			returncouponBean.setUserId(userId);
			returncouponBean.setMainOrderId(orderId);
			returncouponBean.setCouponCodes(CouponCodes);
			returncouponBean.setAppId("app");
			returncouponBean.setRequestId("1233344");			
			
			returncouponCall.setData(returncouponBean);
			returncouponCall.callService();		
			
			couponbatch batch3 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			//job处理
			//Thread.sleep(6000);
			//还券前已使用和接收数量
			Logger.verifyEquals(1, batch1.getUsedCount()-batch2.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(1, batch1.getReceivedCount()-batch2.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");
			//还券后已使用和接收数量
			Logger.verifyEquals(1, batch3.getUsedCount()-batch2.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(1, batch3.getReceivedCount()-batch2.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");
			
			coupon coupon=new couponWapper().selectByBatchId(batch1.getBatchId()).get(0);
			Logger.verifyEquals(false, coupon.getIsUsed(), "验证数据库coupon中IsUsed");
			Logger.verifyEquals(returncouponBean.getMainOrderId(), coupon.getMainOrderId(), "验证数据库coupon中MainOrderId");
			
			Logger.verifyEquals("操作成功！", returncouponCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("0", returncouponCall.getStatusCode1(), "验证返回StatusCode");

			Logger.verifyEquals("null", returncouponCall.getSellerId(), "验证返回sellerId");		
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnCoupon_003() {
		Logger.start(true,"还券-OrderId空");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CouponServiceCallServiceV2 CouponServiceCallServiceV2 = new CouponServiceCallServiceV2();
			
			CreateCouponBean createCouponBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= CouponServiceMCallServiceV2.createPlatformCoupon(createCouponBean);
			UserReceiveCouponCall userReceiveCouponCall = CouponServiceMCallServiceV2.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			String couponCode = userReceiveCouponCall.getUserReceiveResult().getCouponCode();
			
		   couponbatch batch1 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			
			UseCouponBean useCouponBean = CouponServiceCallServiceV2.setUseCouponBean(userId, orderId, couponCode);
			CouponServiceCallServiceV2.useCoupon(useCouponBean);
			
			couponbatch batch2 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(couponCode);
			returncouponBean.setUserId(userId);
			returncouponBean.setCouponCodes(CouponCodes);
			returncouponBean.setAppId("app");
			returncouponBean.setRequestId("1233344");			
			
			returncouponCall.setData(returncouponBean);
			returncouponCall.callService();		
			
			couponbatch batch3 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			coupon coupon=new couponWapper().selectByBatchId(batch1.getBatchId()).get(0);
			Logger.verifyEquals(true, coupon.getIsUsed(), "验证数据库coupon中IsUsed");
			Logger.verifyEquals("参数错误|mainOrderId:userId must", returncouponCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("1", returncouponCall.getStatusCode1(), "验证返回StatusCode");
			
			//还券前已使用和接收数量
			Logger.verifyEquals(1, batch1.getUsedCount()-batch2.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(1, batch1.getReceivedCount()-batch2.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");
			//还券后已使用和接收数量
			Logger.verifyEquals(0, batch3.getUsedCount()-batch2.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(0, batch3.getReceivedCount()-batch2.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnCoupon_004() {
		Logger.start(true,"还券-CouponCode空");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CouponServiceCallServiceV2 CouponServiceCallServiceV2 = new CouponServiceCallServiceV2();
			
			CreateCouponBean createCouponBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= CouponServiceMCallServiceV2.createPlatformCoupon(createCouponBean);
			UserReceiveCouponCall userReceiveCouponCall = CouponServiceMCallServiceV2.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			String couponCode = userReceiveCouponCall.getUserReceiveResult().getCouponCode();
			
		   couponbatch batch1 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			
			UseCouponBean useCouponBean = CouponServiceCallServiceV2.setUseCouponBean(userId, orderId, couponCode);
			CouponServiceCallServiceV2.useCoupon(useCouponBean);
			
			couponbatch batch2 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			
			List<String> CouponCodes=new ArrayList();
			returncouponBean.setUserId(userId);
			returncouponBean.setMainOrderId(orderId);
			returncouponBean.setCouponCodes(CouponCodes);
			returncouponBean.setAppId("app");
			returncouponBean.setRequestId("1233344");				
			
			returncouponCall.setData(returncouponBean);
			returncouponCall.callService();		
			
			couponbatch batch3 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			coupon coupon=new couponWapper().selectByBatchId(batch1.getBatchId()).get(0);
			Logger.verifyEquals(true, coupon.getIsUsed(), "验证数据库coupon中IsUsed");
			Logger.verifyEquals("参数错误|couponCodes:couponcodes must", returncouponCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("1", returncouponCall.getStatusCode1(), "验证返回StatusCode");
			
			//还券前已使用和接收数量
			Logger.verifyEquals(1, batch1.getUsedCount()-batch2.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(1, batch1.getReceivedCount()-batch2.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");
			//还券后已使用和接收数量
			Logger.verifyEquals(0, batch3.getUsedCount()-batch2.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(0, batch3.getReceivedCount()-batch2.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnCoupon_006() {
		Logger.start(true,"还券-userId空");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CouponServiceCallServiceV2 CouponServiceCallServiceV2 = new CouponServiceCallServiceV2();
			
			CreateCouponBean createCouponBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= CouponServiceMCallServiceV2.createPlatformCoupon(createCouponBean);
			UserReceiveCouponCall userReceiveCouponCall = CouponServiceMCallServiceV2.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			String couponCode = userReceiveCouponCall.getUserReceiveResult().getCouponCode();
			
		   couponbatch batch1 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			
			UseCouponBean useCouponBean = CouponServiceCallServiceV2.setUseCouponBean(userId, orderId, couponCode);
			CouponServiceCallServiceV2.useCoupon(useCouponBean);
			
			couponbatch batch2 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(couponCode);
			//returncouponBean.setUserId(userId);
			returncouponBean.setMainOrderId(orderId);
			returncouponBean.setCouponCodes(CouponCodes);
			returncouponBean.setAppId("app");
			returncouponBean.setRequestId("1233344");				
			
			returncouponCall.setData(returncouponBean);
			returncouponCall.callService();		
			
			couponbatch batch3 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			coupon coupon=new couponWapper().selectByBatchId(batch1.getBatchId()).get(0);
			Logger.verifyEquals(true, coupon.getIsUsed(), "验证数据库coupon中IsUsed");
			Logger.verifyEquals("参数错误|userId:userId must", returncouponCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("1", returncouponCall.getStatusCode1(), "验证返回StatusCode");
			
			//还券前已使用和接收数量
			Logger.verifyEquals(1, batch1.getUsedCount()-batch2.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(1, batch1.getReceivedCount()-batch2.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");
			//还券后已使用和接收数量
			Logger.verifyEquals(0, batch3.getUsedCount()-batch2.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(0, batch3.getReceivedCount()-batch2.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnCoupon_005() {
		Logger.start(true,"还券-OrderId与CouponCode不匹配");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CouponServiceCallServiceV2 CouponServiceCallServiceV2 = new CouponServiceCallServiceV2();
			
			CreateCouponBean createCouponBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= CouponServiceMCallServiceV2.createPlatformCoupon(createCouponBean);
			UserReceiveCouponCall userReceiveCouponCall = CouponServiceMCallServiceV2.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			String couponCode = userReceiveCouponCall.getUserReceiveResult().getCouponCode();
			
		   couponbatch batch1 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			
			UseCouponBean useCouponBean = CouponServiceCallServiceV2.setUseCouponBean(userId, orderId, couponCode);
			CouponServiceCallServiceV2.useCoupon(useCouponBean);
			
			couponbatch batch2 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(couponCode);
			returncouponBean.setUserId(userId+1);
			returncouponBean.setMainOrderId(orderId);
			returncouponBean.setCouponCodes(CouponCodes);
			returncouponBean.setAppId("app");
			returncouponBean.setRequestId("1233344");				
			
			returncouponCall.setData(returncouponBean);
			returncouponCall.callService();		
			
			couponbatch batch3 = new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			coupon coupon=new couponWapper().selectByBatchId(batch1.getBatchId()).get(0);
			Logger.verifyEquals(true, coupon.getIsUsed(), "验证数据库coupon中IsUsed");
			Logger.verifyEquals("优惠券已使用但主订单号不一致|优惠券已使用，但不是用在此主订单号上", returncouponCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("21003", returncouponCall.getStatusCode1(), "验证返回StatusCode");
			
			//还券前已使用和接收数量
			Logger.verifyEquals(1, batch1.getUsedCount()-batch2.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(1, batch1.getReceivedCount()-batch2.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");
			//还券后已使用和接收数量
			Logger.verifyEquals(0, batch3.getUsedCount()-batch2.getUsedCount(), "验证数据库couponbatch中UsedCount");
			Logger.verifyEquals(0, batch3.getReceivedCount()-batch2.getReceivedCount(), "验证数据库couponbatch中ReceivedCount");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}